package com.own.core.html.form.dropDown;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.hsqldb.lib.HashMap;

import com.own.core.html.component.ListViewUtil;
import com.own.core.html.panel.OwnDynPanel;
import com.own.core.object.Dao;
import com.own.core.object.lookup.Lookup;
import com.own.core.operation.DbOperation;

public class OwnDropDown extends DropDownChoice {
	boolean change = false;
	DropDown dropDown;
	Field field;

	public OwnDropDown(String id, IModel model, List choices, DropDown dropDown) {
		super(id, model, choices);

		this.dropDown = dropDown;
		init();
		effectOn();
	}

	public OwnDropDown(final String id, IModel model, final List data,
			final IChoiceRenderer renderer, DropDown dropDown) {
		super(id, model, data, renderer);
		this.dropDown = dropDown;
		init();
		effectOn();
	}

	public OwnDropDown(final String id, final List data,
			final IChoiceRenderer renderer) {
		super(id, data, renderer);
		init();
	}

	@Override
	protected boolean wantOnSelectionChangedNotifications() {
		return change;
	}

	public void effectOn() {

		if (dropDown == null || dropDown.effectOn().equals("")) {
			return;
		}

		this.add(new AjaxFormComponentUpdatingBehavior("onchange") {

			@Override
			protected void onUpdate(AjaxRequestTarget target) {

				OwnDropDown thisDropDown = (OwnDropDown) this.getComponent();

				Lookup dao = (Lookup) thisDropDown.getDefaultModelObject();

				ListView listView = (ListView) ((OwnDropDown) this
						.getComponent()).getForm().get("viewList");

				FormComponent formComp = ListViewUtil.getFormComponent(
						listView, dropDown.effectOn());

				OwnDropDown effectDropDown = (OwnDropDown) formComp;
				Map map = new java.util.HashMap();

				map.put(thisDropDown.getField().getName() + ".id", dao.getId());
				List list = DbOperation.getME().listByJPQL(effectDropDown
						.getField().getType(), map);

				effectDropDown.setChoices(list);
				target.addComponent(effectDropDown);
			}
		});

	}

	@Override
	protected void onSelectionChanged(final Object newSelection) {
		Dao dao = (Dao) newSelection;
		ListView listView = (ListView) this.getForm().get("viewList");
		Annotation[] anns = ((OwnDynPanel) this.getParent()).getField()
				.getAnnotations();
		DropDown dropdown = null;
		for (Annotation ann : anns) {
			if (ann instanceof DropDown) {
				dropdown = (DropDown) ann;
				break;
			}

		}

		FormComponent formComp = ListViewUtil.getFormComponent(listView,
				dropdown.effectOn());
		OwnDropDown dd = (OwnDropDown) formComp;
		dd.setChoices(new ArrayList());

	}

	public void init() {

		setOutputMarkupId(true);
		setOutputMarkupPlaceholderTag(true);

	}

	public boolean isChange() {
		return change;
	}

	public void setChange(boolean change) {
		this.change = change;
	}

	public DropDown getDropDown() {
		return dropDown;
	}

	public void setDropDown(DropDown dropDown) {
		this.dropDown = dropDown;
	}

	public Field getField() {
		return field;
	}

	public void setField(Field field) {
		this.field = field;
	}

}
